package com.apexsoft.aas.service.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Class 请求包
 *
 * @author lowezheng
 * @version 1.0.0, 19/01/30
 */
@Deprecated
public class ARequest implements Serializable {

    /**
     * Field  请求序列号
     */
    private String serialNum;

    /**
     * Field  数据包头部
     */
    private Map<String, String> heads;

    /**
     * Field  数据包体
     */
    private Map<String, Object> params;

    /**
     * Field  查询第几页
     */
    private Long currentPage;

    /**
     * Field  每页记录数
     */
    private Long pageSize;


    public static ARequest fromJson(JSONObject json) {
        ARequest req = new ARequest();
        if (json.containsKey("serialNum")) {
            req.setSerialNum(json.getString("serialNum"));
        }
        if (json.containsKey("currentPage")) {
            req.setCurrentPage(json.getLongValue("currentPage"));
        }
        if (json.containsKey("pageSize")) {
            req.setPageSize(json.getLongValue("pageSize"));
        }
        if (json.containsKey("heads")) {
            JSONObject headJson = json.getJSONObject("heads");
            if (headJson != null) {
                req.setHeads(JSONObject.toJavaObject(headJson, Map.class));
            }
        }
        if (json.containsKey("params")) {
            JSONObject paramJson = json.getJSONObject("params");
            if (paramJson != null) {
                req.setParams(JSONObject.toJavaObject(paramJson, Map.class));
            }
        }
        return req;
    }

    public JSONObject toJson() {
        JSONObject json = new JSONObject();
        if (this.serialNum != null) {
            json.put("serialNum", serialNum);
        }
        if (this.currentPage != null) {
            json.put("currentPage", currentPage);
        }
        if (this.pageSize != null) {
            json.put("pageSize", pageSize);
        }
        if (this.heads != null) {
            JSONObject headJson = JSONObject.parseObject(JSON.toJSONString(this.heads));
            json.put("heads", headJson);
        }
        if (this.params != null) {
            JSONObject paramJson = JSONObject.parseObject(JSON.toJSONString(this.params));
            json.put("params", paramJson);
        }
        return json;
    }

    /**
     * GRPC标准-JSON格式字符串，支持Any类型转换
     *
     * @return
     * @throws
     */
    public String toGRPCJsonString() {
        JSONObject json = (JSONObject) JSON.toJSON(this);
        if (this.heads != null) {
            Set<String> keys = heads.keySet();
            JSONObject headJson = new JSONObject();
            for (String key : keys) {
                headJson.put(key, toGrpcAny(heads.get(key)));
            }
            json.remove("heads");
            json.put("head", headJson);
        }
        if (this.params != null) {
            Set<String> keys = params.keySet();
            JSONObject paramJson = new JSONObject();
            for (String key : keys) {
                paramJson.put(key, toGrpcAny(params.get(key)));
            }
            json.remove("params");
            json.put("param", paramJson);
        }
        return json.toJSONString();
    }

    /**
     * Any转换规则，统一转换为StringValue，便于解析
     *
     * @return
     * @throws
     */
    public static JSONObject toGrpcAny(Object value) {
        JSONObject item = new JSONObject();
        item.put("@type", "type.googleapis.com/google.protobuf.StringValue");
        Object valueObject = JSON.toJSON(value);
        item.put("value", valueObject instanceof JSON ? ((JSON) valueObject).toJSONString() : valueObject.toString());
        return item;
    }

    public void addHead(String key, String value) {
        if (heads == null) {
            heads = new HashMap<>();
        }
        heads.put(key, value);
    }

    public void removeHead(String key) {
        if (heads != null) {
            heads.remove(key);
        }
    }

    public void addParam(String key, Object value) {
        if (params == null) {
            params = new HashMap<>();
        }
        params.put(key, value);
    }

    public void removeParam(String key) {
        if (params != null) {
            params.remove(key);
        }
    }

    public <T> T getParamEx(String key, Class<T> cls) {
        if (params != null) {
            Object o = params.get(key);
            //防止JSONObject转成自己
            if (o == null) {
                return null;
            } else if (o instanceof JSONObject && cls != JSONObject.class) {
                return ((JSONObject) o).toJavaObject(cls);
            } else if (o.getClass() == cls) {
                return (T) o;
            } else {
                throw new RuntimeException("key value class: " + o.getClass().getName() + ", not match target class:" + cls.getName());
            }
        } else {
            return null;
        }

    }

    public <T> T getParamEx(Class<T> cls) {
        if (params != null) {
            return (T) new JSONObject(params).toJavaObject(cls);
        }
        return null;

    }

    public String getSerialNum() {
        return serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public Map<String, String> getHeads() {
        return heads;
    }

    public void setHeads(Map<String, String> heads) {
        this.heads = heads;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Long getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Long currentPage) {
        this.currentPage = currentPage;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }
}

